<?php

namespace Store\Controller;

use Think\Controller;

class MemberController extends CommonController {

    //会员扫描二维码处理
    function requestQrcode() {
        header("Content-type: text/html; charset=utf-8");
        $url = I('path.2'); //获取加密参数
        $value = urldecode(base64_decode($url));
        $key = explode('&', $value);
        $qid = str_replace("?id=", "", $key[0]);
        $uid = str_replace("uid=", "", $key[1]);
        $mod = M('Score_qrcode');
        $info = $mod->where('id=' . $qid)->find(); //查询二维码基本信息

        $vm = D('QrcodeproductView');
        $data = $vm->where('Score_qrcode.id=' . $qid)->select();

        foreach ($data as $key => $value) {
            $title .= $value['title'] . '、';
            $pids.= $value['pid'] . '、';
        }
        $order = M('Score_order');
        $where['qid'] = $qid;
        $where['uid'] = $uid;
        //$where['status'] = 1;
        $rs = $order->where($where)->find();
        if ($uid == getwxuserid()) {
            $str = "自己创建的不能参与";
        } else {
            if ($info['status'] == 1) {
                
                $arr['sid'] = $info['sid'];
                $arr['pid'] = rtrim($pids, '、');
                $arr['uid'] = $uid;
                $arr['qid'] = $qid;
                $arr['title'] = rtrim($title, '、');
                $arr['huiyuanname'] = 1;
                $arr['huiyuantel'] = 1;
                $arr['openid'] = wx_openid();
                $arr['price'] = $data[0]['price'];
                $arr['score'] = $data[0]['score'];
                $arr['status'] = 1;
                $arr['ctime'] = $this->crtTime();
                if (!$rs) {
                    $state = $order->add($arr);
                }
                $this->assign('sid', $info['sid']); //赋值商户ID
                $this->assign('uid', $info['uid']); //赋值商户ID
                $this->assign('qid', $qid); //赋值二维码ID
                $this->assign('status', $rs['status']);
            }
            if ($rs['openid'] == 1) {
                $this->assign('info', '已支付');
            } else {
                $this->assign('info', '二维码已失效');
            }
            $this->getQrproductByQid($qid);
            $this->display('index');
        }

        //插入二维码扫描日志
        $arr['qid'] = $qid;
        $arr['ip'] = get_client_ip();
        $arr['mobile'] = $_SERVER ['HTTP_USER_AGENT'];
        $arr['huiyuanid'] = $qid;
        $arr['openid'] = $qid;
        $arr['info'] = $str;
        $arr['huiyuantel'] = $qid;
        $arr['ctime'] = $this->crtTime();
        $qrsan = M('Score_qrscanlog');
        $qrsan->add($arr);
    }

    //确认兑换、支付操作
    function doAdd() {
        $data['status'] = 0;
        header("Content-type: text/html; charset=utf-8");
        $mod = M('Score_order');
        $gc = M('Score_qrcode');
        $data = $gc->where('id=' . $_REQUEST['qid'])->find();
        if (2 == $data['status']) {
            if (1 == $data['openid']) {
                $data['info'] = '付款成功';
                $this->ajaxReturn($data);
            } else {
                $data['info'] = '订单已失效';
                $this->ajaxReturn($data);
            }
        } else {
            $where['qid'] = $_REQUEST['qid'];
            $where['uid'] = $_REQUEST['uid'];
            $where['status'] = 1;
            $state = $mod->where($where)->setField('status', 2);
            if ($state) {
                //减积分操作
                //
                //更新二维码状态
                $udata['status'] = 2;
                $udata['utime'] = $this->crtTime();
                M('Score_qrcode')->where('id=' . $_REQUEST['qid'])->save($udata);
                $data['status'] = 1;
                $this->ajaxReturn($data);
            } else {
                $data['info'] = '付款失败';
                $this->ajaxReturn($data);
            }
        }
    }

}
